package controller

import (
	"api/middleware"
	"api/model"
	"api/utils"
	"api/utils/response"
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
)

/**
 * @Description
 * @Author huan.wang
 * @Date 2021/5/6 9:49 下午
 **/

// AddDevice 添加设备
func AddDevice(ctx *gin.Context)  {
	uid := ctx.Keys["claims"].(*middleware.Claims).UID
	device := model.NewDeviceModel()
	err := ctx.ShouldBindJSON(&device)
	if err != nil {
		response.ErrorResponse(ctx, model.NewMessageResult(500, err.Error()))
		return
	}
	device.Key = utils.GetRandomString(10)
	device.UID = uid
	did, err := device.Insert()
	deviceTopic := model.NewDeviceTopicModel()
	deviceTopic.Topic = fmt.Sprintf("/sys/%s/%s/things/online",device.Name,device.Key)
	deviceTopic.Did = did.(uint)
	deviceTopic.Explain = "在线/离线"
	deviceTopic.Insert()
	deviceTopic.Topic =  fmt.Sprintf("/sys/%s/%s/things/data",device.Name,device.Key)
	deviceTopic.Explain = "数据"
	deviceTopic.Insert()

	if err != nil {
		response.SuccessResponse(ctx, model.NewMessageResult(500, err.Error()))
		return
	}

	deviceSecret := model.NewDeviceSecretModel()
	deviceSecret.DID = did.(uint)
	deviceSecret.Secret = utils.GetRandomString(32)
	deviceSecret.Insert()
	response.SuccessResponse(ctx, model.NewMessageDataResult(200, "success",device.Key))
}

// GetUserDevice 获取用户设备
func GetUserDevice(ctx *gin.Context)  {
	uid := ctx.Keys["claims"].(*middleware.Claims).UID
	device := model.NewDeviceModel()
	device.UID = uid
	devices := device.SelectDeviceByUser()
	response.SuccessResponse(ctx, model.NewMessageDataResult(200, "success",devices))
}

// GetUserDeviceCount 获取用户设备数
func GetUserDeviceCount(ctx *gin.Context)  {
	uid := ctx.Keys["claims"].(*middleware.Claims).UID
	device := model.NewDeviceModel()
	device.UID = uid
	devicesCount := device.SelectDeviceCountByUser()
	response.SuccessResponse(ctx, model.NewMessageDataResult(200, "success",devicesCount))
}

// GetDeviceSecret 获取用户设备秘钥
func GetDeviceSecret(ctx *gin.Context)  {

	did := ctx.Query("id")
	dsm := model.NewDeviceSecretModel()

	param,_ := strconv.ParseUint(did,10,32)
	dsm.DID = uint(param)
	dbdsm := dsm.Select()

	if dbdsm.DID == 0{
		response.SuccessResponse(ctx, model.NewMessageResult(500, "秘钥不存在"))
		return
	}
	response.SuccessResponse(ctx, model.NewMessageDataResult(200, "success",dbdsm))
}

func GetDeviceTypes(ctx *gin.Context)  {

	dtm := model.NewDeviceTypeModel()
	dts := dtm.Select()

	response.SuccessResponse(ctx, model.NewMessageDataResult(200, "success",dts))
}

func DeleteDevice(ctx *gin.Context)  {
	did := ctx.Query("id")
	device := model.NewDeviceModel()
	param,_ := strconv.ParseUint(did,10,32)
	device.ID = uint(param)
	device.Delete()
	response.SuccessResponse(ctx, model.NewMessageResult(200, "success"))
}

func GetDeviceTopic(ctx *gin.Context)  {
	did := ctx.Query("id")
	param,_ := strconv.ParseUint(did,10,32)
	deviceTopic := model.NewDeviceTopicModel()
	deviceTopic.Did = uint(param)
	topics := deviceTopic.SelectByDevice();
	response.SuccessResponse(ctx, model.NewMessageDataResult(200, "success",topics))
}






